package com.ymatou.query.seller.trading.api.service;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymatou.query.seller.trading.api.parameter.JsonOrderBean;
import com.ymatou.query.seller.trading.api.parameter.Seller.GetSellerOrdersByIdsResponse;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.FormatUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 根据多个订单号查询Call FreeMaker Template自动生成代码
 */
public class GetSellerOrdersByIdsCall extends YmatouBaseCall {
	private static String opurl = "api/Seller/GetSellerOrdersByIds";

	public GetSellerOrdersByIdsCall() {
		super(EnvSetup.getSystemUrl("api_seller_order_query"), opurl, "POST",
				"JSON");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept",
				"text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	// get data
	public JSONObject getData() {
		JSONObject data = null;
		try {
			data = new JSONObject(getReturnData()).getJSONObject("Data");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	
	public String getCode(){
		String code = null;
		try {
			code = new JSONObject(getReturnData()).getString("Code");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	public GetSellerOrdersByIdsResponse getSellerOrdersByIdsResponse() {
		return (GetSellerOrdersByIdsResponse) FormatUtil.GSONToObject(getData()
				.toString(), GetSellerOrdersByIdsResponse.class);
	}

	// 返回jsonorder list 对象
	public ArrayList<JsonOrderBean> getOrderList() {
		String returnJson = null;
		ArrayList<JsonOrderBean> orderList = new ArrayList<JsonOrderBean>();
		try {
			returnJson = getData().getString("Orders");
			Gson gson = new Gson();
			orderList = gson.fromJson(returnJson,
					new TypeToken<ArrayList<JsonOrderBean>>() {
					}.getType());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return orderList;
	}

}